package org.dromara.wms.service;

import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.web.core.BaseService;
import org.dromara.sms4j.starter.utils.SpringUtil;
import org.dromara.wms.domain.WmsGroupProduct;
import org.dromara.wms.domain.WmsProduct;
import org.dromara.wms.mapper.WmsGroupProductMapper;
import org.springframework.stereotype.Service;
import org.dromara.wms.domain.WmsGroup;
import org.dromara.wms.mapper.WmsGroupMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 产品分组Service业务层处理
 *
 * @author wenrui Su
 * @date 2024-04-24
 */
@RequiredArgsConstructor
@Service
public class WmsGroupServiceImpl extends BaseService {

    private final WmsGroupMapper baseMapper;

  public WmsGroupProductServiceImpl wmsGroupProductService() {
    return SpringUtil.getBean(WmsGroupProductServiceImpl.class);
  }

  public WmsProductServiceImpl wmsProductService() {
    return SpringUtil.getBean(WmsProductServiceImpl.class);
  }

    /**
     * 查询产品分组
     */
    public WmsGroup queryById(Long id){
        return baseMapper.selectById(id);
    }

    /**
     * 查询产品分组列表
     */
    public TableDataInfo<WmsGroup> queryPageList(WmsGroup bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WmsGroup> lqw = buildQueryWrapper(bo);
        Page<WmsGroup> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询产品分组列表
     */
    public List<WmsGroup> queryList(WmsGroup bo) {
        LambdaQueryWrapper<WmsGroup> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<WmsGroup> buildQueryWrapper(WmsGroup bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WmsGroup> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), WmsGroup::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getSpu()), WmsGroup::getSpu, bo.getSpu());
        lqw.eq(StringUtils.isNotBlank(bo.getIsShop()), WmsGroup::getIsShop, bo.getIsShop());
        lqw.eq(StringUtils.isNotBlank(bo.getIsCrm()), WmsGroup::getIsCrm, bo.getIsCrm());
        lqw.orderByDesc(WmsGroup::getUpdateTime);
        return lqw;
    }

    /**
     * 新增产品分组
     */
    public Boolean insertByBo(WmsGroup bo) {
        validEntityBeforeSave(bo);
        boolean flag = baseMapper.insert(bo) > 0;
        return flag;
    }

    /**
     * 修改产品分组
     */
    public Boolean updateByBo(WmsGroup bo) {
      validEntityBeforeSave(bo);
      WmsGroupProduct wmsGroupProduct = wmsGroupProductService().getById(bo.getId());
      if  (wmsGroupProduct != null ){
        WmsProduct wmsProduct = wmsProductService().queryById(wmsGroupProduct.getProductId());
        if (wmsProduct.getAttrValue() == null && bo.getAttrValueName() == null ||
          wmsProduct.getAttrValue() != null && bo.getAttrValueName() != null){
          Long productCount = wmsGroupProductService().countStr(wmsProduct.getAttrValue());
          Long boCount = wmsGroupProductService().countStr(bo.getAttrValueName());
          if (productCount != boCount){
            throw new WmsStockService.emptyException("当前产品组规格修改后的值与其下对应产品的规格不匹配！");
          }
        }else{
          throw new WmsStockService.emptyException("当前产品组规格修改后的值与其下对应产品的规格不匹配！");
        }
      }
      return baseMapper.updateById(bo) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WmsGroup entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除产品分组
     */
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
